//
//  LyricHelper.m
//  CenturiesMusic
//
//  Created by 漫步人生路 on 15/6/9.
//  Copyright (c) 2015年 漫步人生路. All rights reserved.
//

#import "LyricHelper.h"

@implementation LyricHelper
static LyricHelper *lyricHelper;
+ (LyricHelper *)lyricHelper
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (lyricHelper == nil) {
            lyricHelper = [[LyricHelper alloc]init];
        }
    });
    return lyricHelper;
}

- (void)setSongLrcWithUrl:(NSString *)lrcUrl lycArray:(NSMutableArray *)lycArray timeArray:(NSMutableArray *)timeArray
{
    [lycArray removeAllObjects];
    [timeArray removeAllObjects];
    NSURL *lrcurl = [NSURL URLWithString:lrcUrl];
    NSData *lrcData = [NSData dataWithContentsOfURL:lrcurl];
    NSString *lrcString = [[NSString alloc]initWithData:lrcData encoding:NSUTF8StringEncoding];

    NSArray *lrcMutableArray = [lrcString componentsSeparatedByString:@"\n"];

    for (NSString *everLrc in lrcMutableArray) {
        NSLog(@"%@",everLrc);
        if ([everLrc length] != 0 && [everLrc hasPrefix:@"["] == YES) {
            NSArray *array = [everLrc componentsSeparatedByString:@"]"];
            NSRange rang = [array[0] rangeOfString:@"."];
            if (rang.length > 0) {
                [lycArray addObject:array[1]];
                NSArray *time = [array[0] componentsSeparatedByString:@"["];
                NSString *t = [time objectAtIndex:1];
                NSRange rangTwo = [t rangeOfString:@"."];
                NSString *minutesString = [t componentsSeparatedByString:@":"][0];
                NSString *secondsString = [[t componentsSeparatedByString:@":"][1] componentsSeparatedByString:@"."][0];
                NSString *msString;
                if (rangTwo.length > 0) {
                msString = [[t componentsSeparatedByString:@":"][1] componentsSeparatedByString:@"."][1];
                }else
                {
                    NSLog(@"0-0-0-0-%@",[t componentsSeparatedByString:@":"][2]);

                msString = [t componentsSeparatedByString:@":"][2];
                }

                float minutes = [minutesString intValue];
                float seconds = [secondsString intValue];
                float ms = [msString intValue];
                float timer = minutes * 60 + seconds + ms/100;
                NSString *timerString = [NSString stringWithFormat:@"%.2f",timer];
                [timeArray addObject:timerString];

            }
        }else if ([everLrc hasPrefix:@"["] == NO && [everLrc length] != 0)
        {
            [lycArray addObject:everLrc];
            NSString *noSelec = @"<当前歌词不支持自动滚动>";
            [lycArray insertObject:noSelec atIndex:0];
        }

    }
    
    NSLog(@"时间数组%ld",timeArray.count);
    NSLog(@"歌词数组%ld",lycArray.count);

}
@end
